$(function () {
	$("#jqGrid").jqGrid({
		url: baseURL + 'fun/emp/notList',
		datatype: "json",
		colModel: [
			{ label: '员工ID', name: 'empId', index: "emp_id", width: 30, key: true},
			{ label: '姓名', name: 'empName',sortable: false, width: 100 },
			{ label: '身份证号', name: 'idCard',sortable: false, width: 100 },
			{ label: '所属部门', name: 'deptName', sortable: false, width: 100 },
			{ label: '不在岗原因', name: 'causeStr', width: 100 },
			{ label: '离岗时间', name: 'changeDate', width: 100 },
			{ label: '联系电话', name: 'mobile', width: 100 },
			{ label: '备注', name: 'remarks', index: 'remarks', width: 100 }
		],
		viewrecords: true,
		height: 400,
		rowNum: 10,
		rowList : [10,30,50],
		rownumbers: true,
		rownumWidth: 25,
		autowidth:true,
		multiselect: true,
		pager: "#jqGridPager",
		jsonReader : {
			root: "page.list",
			page: "page.currPage",
			total: "page.totalPage",
			records: "page.totalCount"
		},
		prmNames : {
			page:"page",
			rows:"limit",
			order: "order"
		},
		gridComplete:function(){
			//隐藏grid底部滚动条
			//$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
			$("#jqGrid").setGridParam().hideCol("empId").trigger("reloadGrid");
		},
	});
});
//部门结构树
var dept_ztree;
var dept_setting = {
	data: {
		simpleData: {
			enable: true,
			idKey: "deptId",
			pIdKey: "parentId",
			rootPId: -1
		},
		key: {
			url:"nourl"
		}
	}
};

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			empName: null
		},
		showList: true,
		title: null,
		emp: {
			deptId:null,
			deptName:null,
			gender:null
		},

		pickerOptions: {
			disabledDate(time) {
				return time.getTime() > Date.now();
			}
		},
		politicalBackground: [{value: '群众'},{value:'团员'},{value:'预备党员'},{value:'党员'}],
		education: [{value:'大专'},{value:'本科'},{value:'硕士'},{value:'博士'}],
		statusType: [{value:'0',label:'在岗'}],
		causes: [{value:'7',label:'产假结束'},{value:'8',label:'停薪留职结束'},{value:'9',label:'长病假结束'}],
		fileList: [],
		dialogFormVisible: false,
		dialog: false,
		formLabelWidth: '80px',
		timer: null,
		activeNames: ['1'],
		changeStatusEmpIds:null,
		cause:null,
		status:null,
		changeDate:null,
		name:null,
		attachment:null,
	},

	methods: {
		query: function () {
			vm.reload();
		},

		exportExcel: function (event) {
			var empIds = getSelectedRows();
			if(empIds == null){
				return ;
			}
			$.ajax({
				type: "POST",
				url: baseURL + "fun/emp/exportEmps",
				contentType: "application/json",
				data: JSON.stringify(empIds),
			});
			vm.$message('导出到G://empExcel成功');
		},
		handleCloseDrawer:function(done) {
			vm.dialog=false;
		},
		cancelForm:function() {
			vm.dialog = false;
		},
		handlePreview:function(file) {
			console.log(file);
		},
		handleRemove:function(file, fileList) {
			console.log(file, fileList);
		},
		fresh:function (event)
		{
			$("#jqGrid").trigger("reloadGrid");
			vm.dialogFormVisible=false;
		},
		freshDept:function (event)
		{
			$("#jqGrid").trigger("reloadGrid");
			vm.dialogDeptVisible=false;
		},
		openDialog:function ()
		{
			var empIds = getSelectedRows();
			if(empIds == null){
				return ;
			}
			vm.dialogFormVisible=true;
			vm.status=null;
			vm.cause=null;
			vm.name=null;
			vm.changeDate=null;

		},
		submitUpload() {
			this.$refs.upload.submit();
		},
		handleAvatarSuccess(res, file) {
			vm.attachment = file.response;
		},
		beforeAvatarUpload(file) {
			const isJPG = file.type === 'image/jpeg';
			const isLt2M = file.size / 1024 / 1024 < 2;

			if (!isJPG) {
				this.$message.error('上传图片只能是 JPG 格式!');
			}
			if (!isLt2M) {
				this.$message.error('上传图片大小不能超过 2MB!');
			}
			return isJPG && isLt2M;
		},
		changeStatus:function()
		{
			vm.changeStatusEmpIds= getSelectedRows();
			if(vm.status==null||vm.cause==null)
			{
				alert('状态或原因不能为空');
				return;
			}
			/*关闭dialog*/
			vm.dialogFormVisible=false;
			$.ajax({
				type: "POST",
				url: baseURL + "fun/emp/changeEmpStatus",
				contentType: "application/json",
				data: JSON.stringify({'empIds':vm.changeStatusEmpIds,'status':vm.status,
					'cause':vm.cause,'changeDate':vm.changeDate,
					'name':vm.name,'attachment':vm.attachment}),
					success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});

						}else{
							alert(r.msg);
						}
					}
			});
		},
		getInfo: function(empId){
			$.get(baseURL + "fun/emp/info/"+empId, function(r){
				vm.emp = r.emp;
				vm.getDept();
			});
		},
		getWork: function(empId){
			$.get(baseURL + "fun/emp/work/"+empId, function(r){
				vm.work = r.work;
			});
		},
		getReward: function(empId){
			$.get(baseURL + "fun/emp/reward/"+empId, function(r){
				vm.reward = r.reward;
			});
		},
		getDept: function(){
			//加载部门树
			$.get(baseURL + "sys/dept/list", function(r){
				dept_ztree = $.fn.zTree.init($("#deptTree"), dept_setting , r);
				var node = dept_ztree.getNodeByParam("deptId", vm.emp.deptId);
				if(node != null){
					dept_ztree.selectNode(node);
					vm.emp.deptName = node.name;
				}
			})
		},
		deptTree: function(){
			layer.open({
				type: 1,
				offset: '50px',
				skin: 'layui-layer-molv',
				title: "选择部门",
				area: ['300px', '450px'],
				shade: 0,
				shadeClose: false,
				content: jQuery("#deptLayer"),
				btn: ['确定', '取消'],
				btn1: function (index) {
					var node = dept_ztree.getSelectedNodes();
					//选择上级部门
					vm.emp.deptId = node[0].deptId;
					vm.emp.deptName = node[0].name;
					layer.close(index);
				}
			});
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
				postData:{'name': vm.q.empName,'deptId':  vm.q.deptId},
				page:page
			}).trigger("reloadGrid");
		},
		back: function (event) {
			history.go(-1);
		}
	}
});
window.addEventListener("message",function(event){
	vm.showList = true;
	vm.q.deptId=event.data;
	var page = $("#jqGrid").jqGrid('getGridParam','page');
	$("#jqGrid").jqGrid('setGridParam',{
		postData:{'deptId':event.data,'name': ''},
		page:page
	}).trigger("reloadGrid");
}, false);

